﻿/// <reference path="RequestHandler.js" />
/// <reference path="../HttpRequest.js" />
///--------------------------------------------------------------------
/// <file name="ViewBlogPostHandler.js">
///   Adds functionality to the page where users view blog posts.
/// </file>
///--------------------------------------------------------------------

NAMFox.ns(function() { with (NAMFox) {

this.ViewBlogPostHandler = function() {
};

ViewBlogPostHandler.prototype = new RequestHandler();
ViewBlogPostHandler.prototype.name = "ViewBlogPostHandler";

ViewBlogPostHandler.prototype._onDomNodeInserted = function(event, htmlDocument, _$) {
    var newNode = event.relatedNode;
    
    var results = null;
    if (
        $FX.nodeName(newNode, "li") &&
        (results = newNode.id.match(/^c(\d+)$/))
    ) {
        var textArea = _$("#ec_" + results[1]);
        
        if (textArea.length === 1 && !textArea.val()) {
            htmlDocument.__namfox__.autoComplete.attachSingle(textArea);
            textArea.val($.messages.getDefaultMessage().text);
        }
    }
};

ViewBlogPostHandler.prototype.handleRequestCore = function(request, _$) {
    /// <summary>Handles a request.</summary>
    /// <param name="request" type="HttpRequest">
    ///   The HTTP request which encapsulates the
    ///   document and route data for this request.
    /// </param>

    var markupStripper = new MarkupStripper(
        new BlogMarkupElementSelector(request.htmlDocument, request.routeData.author)
    );
    
    markupStripper.attach();
    
    var defaultCommentTextArea = _$("#comment");
    
    if (defaultCommentTextArea.length === 0) {
        return;
    }
    
    if (!defaultCommentTextArea.val() || defaultCommentTextArea.val() === "Write your comment...") {
        defaultCommentTextArea.val($.messages.getDefaultMessage().text);
    }
    
    var commentList = _$("#comments_list").get(0);
    
    commentList.addEventListener(
        "DOMNodeInserted",
        $.createCallback(this._onDomNodeInserted, this, request.htmlDocument, _$),
        false
    );
};

}});